***********************************************
* Title: rwanda_teacher_baseline_jde.do
* Author: Todd Pugatch
*
* Last update: June 10 2024
* Description: data cleaning for Blimpo and Pugatch, "Entrepreneurship Education
*	and Teacher Training in Rwanda," Stage 2 Registered report, Journal of 
*	Development Economics
*
* Inputs: Teacher_Survey_cleaned_NOPII.dta
* Outputs: teacher_baseline_clean_jde.dta
*
* Notes: cleans teacher baseline survey
************************************************

* Set environment
local start=`"$S_TIME"'
clear
clear matrix
clear mata
graph drop _all
program drop _all
cap log close
set more off

* Set directories
*global main "[SET MAIN DIRECTORY HERE]"
	global rawdata "$main/01_data/01_raw"
	global cleandata "$main/01_data/02_clean"
	global dofiles "$main/02_dofiles"
	global results "$main/03_results"
	global out "$main/04_output"

********************************************************************
* 				       LOAD AND PREPARE DATA
*
*	variable naming and label conventions: 
*	--names:	use [bl/el] suffix for baseline/endline
*				omit if variable is an identifier that could be used to match
*					across datasets, like treatment status or school code
*	--labels: 	use B/E for baseline/endline 
*				use [H/T/S]Q for head teacher/teacher/student questionnaire
*	--example: enrollment, as reported by head teacher at baseline in item #123
*		name: enroll_bl
*		label: "BHQ123: enrollment"
*
********************************************************************
qui use "$rawdata/teacher/Teacher_Survey_cleaned_NOPII.dta", clear

/*correct coding errors, according to "Manual Checking Report.docx"*/
/*update discrepancies between treatment status between sampling spreadsheet and dataset*/
qui replace group_111=2 if school_code_107=="0301112" 	
qui replace group_111=1 if school_code_107=="0403110" 	
qui replace group_111=1 if school_code_107=="0403037" 	
qui replace group_111=1 if school_code_107=="0403050" 	
qui replace group_111=1 if school_code_107=="0305012"	
qui replace group_111=1 if school_code_107=="0504114"	

qui destring school_code_107, gen(school_code) force
qui gen schoolname_bl=school_name_106
lab var schoolname_bl "school name, as reported in baseline (school_name_106)"
lab val schoolname_bl schoollab

************************************************************************
*											   						   *
* 	       SECTION 1: Identifying information			   			   *
* 											   						   *
************************************************************************
/*survey identifying information: need to mark as baseline*/
local X "interview_starttime interview_endtime formdef_version key submissiondate starttime endtime" 
foreach x in `X' {
	qui gen `x'_bl=`x'
	lab var `x'_bl "`x', baseline"
}

/*create district & province IDs to match Census/map codes*/
qui gen district_id=.
qui replace district_id=57 if district_101==1
qui replace district_id=53 if district_101==2 
qui replace district_id=54 if district_101==3 
qui replace district_id=56 if district_101==4 
qui replace district_id=42 if district_101==5 
qui replace district_id=45 if district_101==6 
qui replace district_id=43 if district_101==7 
qui replace district_id=41 if district_101==8 
qui replace district_id=31 if district_101==9 
qui replace district_id=35 if district_101==10 
qui replace district_id=34 if district_101==11 
lab var district_id "District ID, Census code"

qui gen province_id=.
qui replace province_id=5 if province_100==1 /*Eastern*/
qui replace province_id=4 if province_100==2 /*Northern*/
qui replace province_id=3 if province_100==3 /*Western*/
lab var province_id "Province ID, Census code"

/*school type*/
qui gen public=(publ_priv_108==1)
qui gen private=(publ_priv_108==2)
qui gen private_pubaided=(publ_priv_108==3)
qui gen nonpublic=(publ_priv_108==2|publ_priv_108==3)
qui gen treatment=(group_111==1)
qui replace treatment=. if group_111==.
sort district_101 public
qui egen strata=group(district_101 public)
lab def treatment 0 "control" 1 "treatment"
lab val treatment treatment 
lab var public "BTQ108: public school"
lab var private "BTQ108: private school"
lab var private_pubaided "BTQ108: part public, part private school"
lab var nonpublic "BTQ108: non-public school"
lab var strata "stratification ID (district and public/non-public)"

/*alternate versions of treatment (see "Manual Checking Report.docx" for more details)*/
* alternate version 1: code unassigned replacement schools as missing
qui gen treatment_unassgn=treatment
qui replace treatment_unassgn=. if school_code_107=="0305112"	
qui replace treatment_unassgn=. if school_code_107=="0304102"	
qui replace treatment_unassgn=. if school_code_107=="0403121"	
qui replace treatment_unassgn=. if school_code_107=="0503118"	
lab var treatment_unassgn "treatment status (unassigned replacement schools as missing)"

* alternative version 2: code schools with mismatched status between E! and initial assignment
qui gen treatment_educateassgnt=treatment
qui replace treatment_educateassgnt=1 if school_code_107=="0301112" 	
qui replace treatment_educateassgnt=0 if school_code_107=="0403110" 	
qui replace treatment_educateassgnt=0 if school_code_107=="0403037" 	

/*additional mismatches based on E! error, not IPA assignment choice*/
qui replace treatment_educateassgnt=1 if school_code_107=="0503014" 	
qui replace treatment_educateassgnt=0 if school_code_107=="0503104" 	
lab var treatment_educateassgnt "treatment status (based on Educate! records, not initial assignment)"
foreach x in unassgn educateassgnt {
	lab val treatment_`x' treatment
}

/*teach entrepreneurship*/
qui gen entrepreneurship_S4_bl=teach_entr_117
qui gen entrepreneurship_S5_bl=teach_entr_119
qui gen entrepreneurship_S6_bl=teach_entr_120
qui gen entrepreneurship_S56_bl=(entrepreneurship_S5_bl==1|entrepreneurship_S6_bl==1)
forval g=4/6 {
	lab var entrepreneurship_S`g'_bl "BTQ117/119/120: teach S`g' entrepreneurship"
	qui gen teach_otherS`g'_bl=((oth_subj_s`g'_122>=1 & oth_subj_s`g'_122<=14 & oth_subj_s`g'_122!=.)|oth_subj_s`g'_122==-66)
	lab var teach_otherS`g'_bl "BTQ122: teach subject other than entrepreneurship, S`g'"	
}
qui egen teach_other_bl=anymatch(teach_otherS4_bl teach_otherS5_bl teach_otherS6_bl), v(1)
lab var entrepreneurship_S56_bl "BTQ119/120: teach S5 or S6 entrepreneurship"
lab var teach_other_bl "BTQ122: teach subject other than entrepreneurship"

* generate a teacher ID variable
/*No teacher ID assigned at baseline. However, only one teacher per school surveyed at baseline. In endline,
	surveyed teacher who were also surveyed at baseline were given ID with final digit "1." Specifically,
	they were assigned teacherid = schoolid*10 + 1.*/
qui gen long teacherid=school_code*10+1
qui gen teacherid_bl=teacherid
lab var teacherid_bl "teacher ID, based on baseline school_code"
	
************************************************************************
*											   						   *
* 	       SECTION 2: Teacher experience and training	   			   *
* 											   						   *
************************************************************************
/*gender & age*/
qui gen female_bl=gender_200
qui gen age_bl=age_201
qui gen age_miss_bl=(age_201==.)
lab var female_bl "BTQ200: female"
lab var age_bl "BTQ201: age"
lab var age_miss_bl "BTQ201: missing value for age"
 
/*position: skip because only 3 are head teacher or deputy head teacher*/
/*qualified, contract teacher*/
qui gen qualified_bl=(qual_203==1)
qui gen contractteacher_bl=(contr_205==1)
lab var qualified_bl "BTQ203: qualified teacher"
lab var contractteacher_bl "BTQ205: contract teacher (on 1-yr contract)"

/*teaching experience & tenure at current school*/
qui gen experience_bl=2016-yrbeginteach_206
qui gen tenure_bl=2016-yrteachhere_207
lab var experience_bl "BTQ206: teaching experience"
lab var tenure_bl "BTQ207: tenure at current school"

/*S4 entrepreneurship class size*/
foreach x in male female {
	qui gen classsize_`x'_bl=`x'stud_212
	lab var classsize_`x'_bl "BTQ212: S4 entrepreneurship class size, `x'"
}
qui egen classsize_total_bl=rowtotal(classsize_male_bl classsize_female_bl)
lab var classsize_total_bl "BTQ212: S4 entrepreneurship class size"

/*education & training*/
/*also BTQ218: year of pre-service training*/
qui gen bachelors_bl=(educ_216==3|educ_216==4)
qui gen preservice_2yr_bl=(train_217==1)
qui gen preservice_4yr_bl=(train_217==2)
qui gen preservice_current_bl=(train_217==3)
qui gen inservice_attend_bl=(attended_219==1)
*qui gen yr=year(yreduc_219)
gen yr = substr(yreduc_219, -4, 4)
destring(yr), replace
qui gen inservice_attend_2016_bl=(yr==2016)
qui gen inservice_attend_2015_bl=(yr==2015)
qui gen inservice_attend_2014orbefore_bl=(yr<=2014 & yr!=.)
qui gen inservice_attend_dknever_bl=(yr==.)
qui gen inservice_attend_yr_bl=.
qui replace inservice_attend_yr_bl=1 if inservice_attend_2016_bl==1
qui replace inservice_attend_yr_bl=2 if inservice_attend_2015_bl==1
qui replace inservice_attend_yr_bl=3 if inservice_attend_2014orbefore_bl==1
qui replace inservice_attend_yr_bl=0 if inservice_attend_dknever_bl==1
lab def inservice_attend_yr_bl 0 "don't know/never" 1 "2016" 2 "2015" 3 "2014 or before"
lab val inservice_attend_yr_bl inservice_attend_yr_bl
qui gen train_curriculum_bl=(traincurr_220==1)
qui gen train_entr_bl=(trainentr_221==1)
qui gen train_obs_bl=(obstrain_222==1|othobstrain_223==1)
lab var bachelors_bl "BTQ216: bachelor's degree (A0) or higher"
lab var preservice_2yr_bl "BTQ217: 2 years of pre-service training"
lab var preservice_4yr_bl "BTQ217: 4 years of pre-service training"
lab var preservice_current_bl "BTQ217: currently in teacher training college"
lab var inservice_attend_bl "BTQ219: attended in-service training program"
lab var inservice_attend_2016_bl "BTQ219: last in-service training attended: 2016"
lab var inservice_attend_2015_bl "BTQ219: last in-service training attended: 2015"
lab var inservice_attend_2014orbefore_bl "BTQ219: last in-service training attended: 2014 or earlier"
lab var inservice_attend_dknever_bl "BTQ219: last in-service training attended: don't know/never"
lab var inservice_attend_yr_bl "BTQ219: year of last in-service training"
lab var train_curriculum_bl "BTQ220: attended training on competency-based curriculum"
lab var train_entr_bl "BTQ221: attended training on entrepreneurship"
lab var train_obs_bl "BTQ221: observed or observed by another teacher in last term"
drop yr

/*lesson plans*/
/*also BTQ226-229: staff code of conduct, staff mtgs*/
qui gen entre_lessonplan_shown_bl=(writtentr_224==2)
qui gen entre_lessonplan_notshown_bl=(writtentr_224==1)
qui gen entre_lessonplan_none_bl=(writtentr_224==0)
qui gen lessonnotes_shown_bl=(writtlesso_225==2)
qui gen lessonnotes_notshown_bl=(writtlesso_225==1)
qui gen lessonnotes_none_bl=(writtlesso_225==0)
qui gen entre_lessons_6plus_bl=(nbrlesso_230>=6)
lab var entre_lessonplan_shown_bl "BTQ224: has written entrepreneurship lesson plan (shown)"
lab var entre_lessonplan_notshown_bl "BTQ224: has written entrepreneurship lesson plan (not shown)"
lab var entre_lessonplan_none_bl "BTQ224: does not have written entrepreneurship lesson plan"
lab var lessonnotes_shown_bl "BTQ225: has lesson notes (shown)"
lab var lessonnotes_notshown_bl "BTQ225: has lesson notes (not shown)"
lab var lessonnotes_none_bl "BTQ225: does not have lesson notes"
lab var entre_lessons_6plus_bl "BTQ230: 6 or more entrepreneurship lessons per week"

qui egen lessonplan_index_bl=rowmean(entre_lessonplan_shown_bl lessonnotes_shown_bl)
lab var lessonplan_index_bl "BTQ224-225: mean of dummies for showing lesson plan & lesson notes"


************************************************************************
*											   						   *
* 	           SECTION 3: Pedagogy	   			   				       *
* 											   						   *
************************************************************************
/* classify these methods as "active learning":
	--question & answer
	--small group work
	--games
	--activities outside classroom
	--experiment
	--student portfolio
************************************************************************/	
	
/*code REMOVES discussion and practice exercises, as these did not appear in pre-analysis plan or JDE Registered Report*/
local active_mostcomf "meth_300_mostcomf_QandA meth_300_mostcomf_GroupWork meth_300_mostcomf_Games meth_300_mostcomf_ActOutClass meth_300_mostcomf_Experiment meth_300_mostcomf_Portfolio"
local active_leastcomf "meth_301_leastcomf_QandA meth_301_leastcomf_GroupWork meth_301_leastcomf_Games meth_301_leastcomf_ActOutClass meth_301_leastcomf_Experiment meth_301_leastcomf_Portfolio"
local active_entr "meth_302_lastused_QandA meth_302_lastused_GroupWork meth_302_lastused_Games meth_302_lastused_ActOutClass meth_302_lastused_Experiment meth_302_lastused_Portfolio"
local active_exam_mostef "meth_307_mostef_QandA meth_307_mostef_GroupWork meth_307_mostef_Games meth_307_mostef_ActOutClass meth_307_mostef_Experiment meth_307_mostef_Portfolio"
local active_exam_leastef "meth_308_leasttef_QandA meth_308_leasttef_GroupWork meth_308_leasttef_Games meth_308_leasttef_ActOutClass meth_308_leasttef_Experiment meth_308_leasttef_Portfolio"
local active_entr_mostef "meth_316_mostef_QandA meth_316_mostef_GroupWork meth_316_mostef_Games meth_316_mostef_ActOutClass meth_316_mostef_Experiment meth_316_mostef_Portfolio"
local active_entr_leastef "meth_317_leasttef_QandA meth_317_leasttef_GroupWork meth_317_leasttef_Games meth_317_leasttef_ActOutClass meth_317_leasttef_Experiment meth_317_leasttef_Portfolio"

foreach x in mostcomf leastcomf entr exam_mostef exam_leastef entr_mostef entr_leastef {		
	qui egen pedagogy_active_`x'_bl=rowtotal(`active_`x'')
	notes pedagogy_active_`x'_bl: "active includes Q&A, small group work, games, class discussion, activities outside classroom, practice exercises, experiment, student portfolio"
}
lab var pedagogy_active_mostcomf_bl "BTQ300: # active learning strategies among 3 most comfortable methods"
lab var pedagogy_active_leastcomf_bl "BTQ301: # active learning strategies among 3 least comfortable methods"	
lab var pedagogy_active_entr_bl "BTQ302: # active learning strategies used in last entrepreneurship class"
lab var pedagogy_active_exam_mostef_bl "BTQ307: # active learning strategies among 5 most effective for passing exam"
lab var pedagogy_active_exam_leastef_bl "BTQ308: # active learning strategies among 5 least effective for passing exam"
lab var pedagogy_active_entr_mostef_bl "BTQ316: # active learning strategies among 3 most effective for entrepreneurship"
lab var pedagogy_active_entr_leastef_bl "BTQ317: # active learning strategies among 3 least effective for entrepreneurship"

qui gen pedagogy_active_bl=(pedagogy_active_mostcomf_bl>=2 & pedagogy_active_mostcomf_bl!=.)
qui gen pedagogy_active_index_bl=pedagogy_active_mostcomf_bl + (3-pedagogy_active_leastcomf_bl) + pedagogy_active_entr_bl
lab var pedagogy_active_bl "BTQ300: interactive pedagogy at least 2 of 3 most comfortable methods"
lab var pedagogy_active_index_bl "BTQ300-302: interactive pedagogy index (0=least interactive, 12=most)"

/***********************************************************
alternative classification 
* classify these methods as "active learning":
	--small group work
	--games
	--research
	--activities outside classroom
	--practice exercises
	--experiment
	--student portfolio
***********************************************************/

local active2_mostcomf "meth_300_mostcomf_GroupWork meth_300_mostcomf_Games meth_300_mostcomf_Research meth_300_mostcomf_ActOutClass meth_300_mostcomf_PracticeEx meth_300_mostcomf_Experiment meth_300_mostcomf_Portfolio"
local active2_leastcomf "meth_301_leastcomf_GroupWork meth_301_leastcomf_Games meth_301_leastcomf_Research meth_301_leastcomf_ActOutClass meth_301_leastcomf_PracticeEx meth_301_leastcomf_Experiment meth_301_leastcomf_Portfolio"
local active2_entr "meth_302_lastused_GroupWork meth_302_lastused_Games meth_302_lastused_Research meth_302_lastused_ActOutClass meth_302_lastused_PracticeEx meth_302_lastused_Experiment meth_302_lastused_Portfolio"
local active2_exam_mostef "meth_307_mostef_GroupWork meth_307_mostef_Games meth_307_mostef_ActOutClass meth_307_mostef_PracticeEx meth_307_mostef_Research meth_307_mostef_Experiment meth_307_mostef_Portfolio"
local active2_exam_leastef "meth_308_leasttef_GroupWork meth_308_leasttef_Games meth_308_leasttef_Research meth_308_leasttef_ActOutClass meth_308_leasttef_PracticeEx meth_308_leasttef_Experiment meth_308_leasttef_Portfolio"
local active2_entr_mostef "meth_316_mostef_GroupWork meth_316_mostef_Games meth_316_mostef_Research meth_316_mostef_ActOutClass meth_316_mostef_PracticeEx meth_316_mostef_Experiment meth_316_mostef_Portfolio"
local active2_entr_leastef "meth_317_leasttef_GroupWork meth_317_leasttef_Games meth_317_leasttef_Discussion meth_317_leasttef_ActOutClass meth_317_leasttef_PracticeEx meth_317_leasttef_Experiment meth_317_leasttef_Portfolio"

foreach x in mostcomf leastcomf entr exam_mostef exam_leastef entr_mostef entr_leastef {		
	qui egen pedagogy_active2_`x'_bl=rowtotal(`active2_`x'')
	notes pedagogy_active2_`x'_bl: "active (E! definition) includes small group work, games, research, activities outside classroom, practice exercises, experiment, student portfolio"
}
lab var pedagogy_active2_mostcomf_bl "BTQ300: # active learning strategies among 3 most comfortable methods (E! definition)"
lab var pedagogy_active2_leastcomf_bl "BTQ301: # active learning strategies among 3 least comfortable methods (E! definition)"	
lab var pedagogy_active2_entr_bl "BTQ302: # active learning strategies used in last entrepreneurship class (E! definition)"
lab var pedagogy_active2_exam_mostef_bl "BTQ307: # active learning strategies among 5 most effective for passing exam (E! definition)"
lab var pedagogy_active2_exam_leastef_bl "BTQ308: # active learning strategies among 5 least effective for passing exam (E! definition)"
lab var pedagogy_active2_entr_mostef_bl "BTQ316: # active learning strategies among 3 most effective for entrepreneurship (E! definition)"
lab var pedagogy_active2_entr_leastef_bl "BTQ317: # active learning strategies among 3 least effective for entrepreneurship (E! definition)"
	
/*entrepreneurship curriculum*/
qui gen businessclub_bl=(entr_club_304==1)
qui gen competencebased_use_bl=(usecomp_310==1)
qui gen competencebased_usealways_bl=(freq_comp_311==1)
qui gen competencebased_rebtrain_bl=(learn_comp_312==1)
qui gen skillslab_aware_bl=(skilllab_314==2)
qui gen skillslab_definition_bl=(def_skilllab_315==2)
qui gen skillslab_definition_cond_bl=skillslab_definition_bl
qui replace skillslab_definition_cond_bl=. if skillslab_aware==0
qui gen competencebased_assess_bl=(assess_318_Answer==1)
lab var businessclub_bl "BTQ304: school has business club"
lab var competencebased_use_bl "BTQ310: Uses competence-based approach"
lab var competencebased_usealways_bl "BTQ311: Uses competence-based approach every lesson"
lab var competencebased_rebtrain_bl "BTQ312: Learned competence-based approach through REB training"
lab var skillslab_aware_bl "BTQ314: aware of Skills Lab"
lab var skillslab_definition_bl "BTQ315: knows definition of Skills Lab"
lab var skillslab_definition_cond_bl "BTQ315: knows definition of Skills Lab (conditional on awareness)"
lab var competencebased_assess_bl "BTQ318: important to assess higher-order thinking skills"

************************************************************************
*											   						   *
* 	           SECTION 4: Entrepreneurship curriculum	   			   *
* 											   						   *
************************************************************************
* SECTION 4: Entrepreneurship curriculum
qui egen mrktskl_true_bl=anycount(mrktskl_400_DevProBrand mrktskl_400_Advert mrktskl_400_VerbalProm), v(1)
qui egen mrktskl_false_bl=anycount(mrktskl_400_KeepingAccount), v(1)
qui gen mrktskl_true_pct_bl=mrktskl_true_bl/3
qui gen mrktskl_false_pct_bl=mrktskl_false_bl
qui egen buspln_true_bl=anycount(busplnskl_401_BusiName busplnskl_401_MarketgPlan), v(1)
qui egen buspln_false_bl=anycount(busplnskl_401_ReceiFromSales busplnskl_401_PandLStatemt busplnskl_401_Audit), v(1)
qui gen buspln_true_pct_bl=buspln_true_bl/2
qui gen buspln_false_pct_bl=buspln_false_bl/3
qui gen profit_calculation_bl=(proftamt_402==30000)
qui egen incrproft_true_bl=anycount(incrproft_403_SellMorePaper incrproft_403_IncreasPrice incrproft_403_UseCheaperMat), v(1)
qui egen incrproft_false_bl=anycount(incrproft_403_EmploySone), v(1)
qui gen incrproft_true_pct_bl=incrproft_true_bl/3
qui gen incrproft_false_pct_bl=incrproft_false_bl
qui gen busgrwth_indic_bl=(notbusgrth_404==4)
qui gen profit_definition_bl=(knownoproft_405==1)
lab var mrktskl_true_bl "BTQ400: number of true marketing skills identified (of 3)" 	
lab var mrktskl_false_bl "BTQ400: identified false marketing skill (of 1)" 
lab var mrktskl_true_pct_bl "BTQ400: % of true marketing skills identified (of 3)" 	
lab var mrktskl_false_pct_bl "BTQ400: % of false marketing skills identified (of 1)" 
lab var buspln_true_bl "BTQ401 number of true business plan elements identified (of 2)" 	
lab var buspln_false_bl "BTQ401: number of false business plan elements identified (of 3)" 
lab var buspln_true_pct_bl "BTQ401: % of true business plan elements identified (of 2)" 	
lab var buspln_false_pct_bl "BTQ401: % of false business plan elements identified (of 3)" 
lab var profit_calculation_bl "BTQ402: can calculate profit from example"
lab var incrproft_true_bl "BTQ403: number of true ways to increase profit identified (of 3)" 	
lab var incrproft_false_bl "BTQ403: number of false ways to increase profit identified (of 1)" 
lab var incrproft_true_pct_bl "BTQ403: % of true true ways to increase profit identified (of 3)" 	
lab var incrproft_false_pct_bl "BTQ403: % of false true ways to increase profit identified (of 1)"
lab var busgrwth_indic_bl "BTQ404: correctly identifies indicators of business growth"
lab var profit_definition_bl "BTQ405: understands definition of profit"

qui egen eknowledge_index_bl=rowmean(mrktskl_true_pct_bl buspln_true_pct_bl profit_calculation_bl incrproft_true_pct_bl busgrwth_indic_bl profit_definition_bl)
lab var eknowledge_index_bl "BTQ400-405: mean of entrepreneurship knowledge questions"

/*teaching improvement and challenges*/
* see qual_impr_319_* series for 2 most important ways to improve lesson quality
* see chal_320_* series for 2 biggest challenges to teaching good lessons
* see mat_321_* series for materials available for competence-based curriculum

************************************************************************
*											   						   *
* 			   		SECTION 5: Job duties	   				   		   *
* 											   						   *
************************************************************************
/*also 	BTQ501: time outside school
		BTQ504: reasons for absence
		BTQ505: non-teaching duties
		BTQ507-8: date & topic of last parent-teacher conference*/		
qui gen absenceslastmth_bl=timesaway_500
qui gen absenceslastmth_ifabsent_bl=timesaway_500
qui replace absenceslastmth_bl=0 if timesaway_500==.
forval i=1/9 {
	qui gen activ`i'_502r=activ`i'_502
	qui replace activ`i'_502r=0 if activ`i'_502==.|activ`i'_502<0
}
aorder activ?_502r
qui egen totaltime=rowtotal(activ1_502r-activ9_502r)
qui gen teachingtime_shr_bl=activ1_502r/totaltime
qui gen teachandpreptime_shr_bl=(activ1_502r+activ2_502r+activ3_502r+activ9_502r)/totaltime
qui gen substitute_bl=(timeoth_503>0 & timeoth_503!=.)
qui gen parentinvolve_bl=(reguparent_506>1 & reguparent_506!=.)
lab var absenceslastmth_bl "BTQ500: number of absences last month"
lab var absenceslastmth_ifabsent_bl "BTQ500: number of absences last month (0=missing)"
lab var teachingtime_shr_bl "BTQ502: time spent teaching as share of working day"
lab var teachandpreptime_shr_bl "BTQ502: time spent teaching (inc. prep, grading, extra classes) as share of working day"
lab var substitute_bl "BTQ503: must substitute for other teachers at least once per month"
lab var parentinvolve_bl "BTQ506: parents regularly ask about children"
drop totaltime

************************************************************************
*											   						   *
* 			SECTION 6: Teacher income and job satisfaction	   		   *
* 											   						   *
************************************************************************
/*also BTQ604-5: awards available for teaching*/
qui gen otherjob_bl=(othoccup_600==1)
qui gen otherjobhrs_bl=hrsothoccup_601 if otherjob_bl==1 & hrsothoccup_601>=0
qui gen otherjobinc_bl=monthinc_602 if otherjob_bl==1
qui gen privatetutor_bl=(aftclass_603==1)
qui gen satisfied_income_bl=(point1_606==3|point1_606==4)
qui gen satisfied_workload_bl=(point2_606==3|point2_606==4)
qui gen satisfied_post_bl=(point3_606==3|point3_606==4)
qui gen satisfied_jobsecurity_bl=(point4_606==3|point4_606==4)
qui gen satisfied_appreciation_bl=(point5_606==3|point5_606==4)
qui gen satisfied_status_bl=(point6_606==3|point6_606==4)
qui gen satisfied_workenv_bl=(point7_606==3|point7_606==4)
qui gen satisfied_support_bl=(point8_606==3|point8_606==4)
qui gen satisfied_overall_bl=(point9_606==3|point9_606==4)
lab var otherjob_bl "BTQ600: holds other job for pay besides teaching"
lab var otherjobhrs_bl "BTQ601: hours per week in non-teaching job"
lab var otherjobinc_bl "BTQ602: income from non-teaching job"
lab var privatetutor_bl "BTQ603: works as private tutor for pay"
lab var satisfied_income_bl "BTQ606: satisfied or very satisfied with job income"
lab var satisfied_workload_bl "BTQ606: satisfied or very satisfied with job workload"
lab var satisfied_post_bl "BTQ606: satisfied or very satisfied with job posting"
lab var satisfied_jobsecurity_bl "BTQ606: satisfied or very satisfied with job security"
lab var satisfied_appreciation_bl "BTQ606: satisfied or very satisfied with job appreciation from students/parents"
lab var satisfied_status_bl "BTQ606: satisfied or very satisfied with job social status"
lab var satisfied_workenv_bl "BTQ606: satisfied or very satisfied with work environment"
lab var satisfied_support_bl "BTQ606: satisfied or very satisfied with job support from government"
lab var satisfied_overall_bl "BTQ606: satisfied or very satisfied with job overall"

* save data
qui gen insample_bl=1
lab var insample_bl "in baseline sample"


qui compress
lab data "Teacher baseline survey (2016), modified from cleaned data"
qui save "$cleandata/teacher_baseline_clean_jde.dta", replace	

local end=`"$S_TIME"' 
di "`start'"
di "`end'"
